extends Node
class_name IsInCheck

var validate:ValidatorController

## 设置移动校验控制器
func set_validator(validator:ValidatorController) -> void:
    validate = validator

## 检查指定方是否将军或被将军
func check_any_check(side:BoardCommon.TURN, pieces:Pieces) -> bool:
    var enemy_side = BoardCommon.TURN.BLACK if side == BoardCommon.TURN.RED else BoardCommon.TURN.RED
    
    var allPieces:Array[PieceV2] = pieces.all_pieces()
    # 检查双方将/帅位置
    var my_general_pos:Vector2i
    var enemy_general_pos:Vector2i
    
    for piece in allPieces:
        if piece.type == BoardCommon.TYPE.GENERAL:
            if piece.side == side:
                my_general_pos = BoardCommon.pos_to_grid_pos(piece.position)
            else:
                enemy_general_pos = BoardCommon.pos_to_grid_pos(piece.position)
    
    # 检查本方是否将军对方
    for piece in allPieces:
        if piece.side == side:
            var piece_pos:Vector2i = BoardCommon.pos_to_grid_pos(piece.position)
            if validate.validate_piece(piece, piece_pos, enemy_general_pos):
                print("将军对方！")
                return true
    
    # 检查本方是否被将军
    for piece in pieces.all_pieces():
        if piece.side == enemy_side:
            var piece_pos:Vector2i = BoardCommon.pos_to_grid_pos(piece.position)
            if validate.validate_piece(piece, piece_pos, my_general_pos):
                print("被将军！")
                return true
    
    return false